package com.sui.bigdata.sml.web.controller;

import com.sui.bigdata.sml.web.dto.ComponentCategoryDTO;
import com.sui.bigdata.sml.web.dto.ComponentDTO;
import com.sui.bigdata.sml.web.dto.NodeDTO;
import com.sui.bigdata.sml.web.service.ComponentService;
import com.sui.bigdata.sml.web.service.NodeService;
import com.sui.bigdata.sml.web.vo.CategoryDependenciesInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author songhaicheng
 * @date 2019/11/7 10:07
 * @description
 * @reviewer
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/web/ui/components")
@Slf4j
public class ComponentController {

    @Autowired
    private ComponentService service;
    @Autowired
    private NodeService nodeService;

    @PostMapping
    ResponseEntity add(@RequestBody ComponentDTO dto) {
        return new ResponseEntity<>(dto.setId(service.add(dto)), HttpStatus.CREATED);
    }

    @GetMapping("/{id}")
    ResponseEntity get(@PathVariable("id") Long id) {
        return new ResponseEntity<>(service.get(id), HttpStatus.OK);
    }

    @GetMapping
    ResponseEntity list() {
        return new ResponseEntity<>(service.listAll(), HttpStatus.OK);
    }

    @PutMapping("/{id}")
    ResponseEntity update(@PathVariable("id") Long id, @RequestBody ComponentDTO dto) {
        dto.setId(id);
        service.update(dto);
        return new ResponseEntity<>(dto, HttpStatus.OK);
    }

    @DeleteMapping("/{id}")
    ResponseEntity delete(@PathVariable("id") Long id) {
        service.delete(id);
        log.info("组件删除成功，id：{}", id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @GetMapping("/{id}/dependencies")
    ResponseEntity getCategoryDependenciesInfo(@PathVariable("id") Long id) {
        ComponentDTO component = service.get(id);
        // 所有已知属于该分类的节点
        List<NodeDTO> nodes = nodeService.listByComponent(component);
        long affectedNodeNum = nodes.size();
        // 提取出所有实验 Id
        long affectedExpNum = nodes.stream().map(NodeDTO::getExperimentId).distinct().count();
        CategoryDependenciesInfoVO info = new CategoryDependenciesInfoVO()
                .setAffectedNodeNum(affectedNodeNum)
                .setAffectedExpNum(affectedExpNum);
        return new ResponseEntity<>(info, HttpStatus.OK);
    }

    @PostMapping("/categories")
    ResponseEntity addCategory(@RequestBody ComponentCategoryDTO dto) {
        return new ResponseEntity<>(dto.setId(service.addCategory(dto)), HttpStatus.CREATED);
    }

    @PutMapping("/categories/{id}")
    ResponseEntity updateCategory(@PathVariable("id") Integer id, @RequestBody ComponentCategoryDTO dto) {
        dto.setId(id);
        service.updateCategory(dto);
        return new ResponseEntity<>(dto, HttpStatus.OK);
    }

    @GetMapping("/categories")
    ResponseEntity listCategories() {
        return new ResponseEntity<>(service.listAllCategories(), HttpStatus.OK);
    }

}
